package com.xiaofeinode.service.impl;

import com.xiaofeinode.mapper.UserMapper;
import com.xiaofeinode.pojo.User;
import com.xiaofeinode.service.UserService;
import com.xiaofeinode.utils.MybatisUtils;
import com.xiaofeinode.utils.ResultUtils;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

public class UserServiceImpl implements UserService {
    UserMapper userMapper = MybatisUtils.getSqlSession().getMapper(UserMapper.class);

    @Override
    public ResultUtils add(String username,String password) {
        Integer add = userMapper.add(new User(null,username, password));
        if (add>0){
            return new ResultUtils(200, "添加成功", null);
        }
        return  new ResultUtils(500, "添加失败", null);
    }

    @Override
    public ResultUtils delete(Integer id) {
        Integer delete = userMapper.delete(id);
        if (delete>0){
            return new ResultUtils(200,"删除成功",null);
        }
        return  new ResultUtils(500,"删除失败",null);
    }

    @Override
    public ResultUtils update(Integer id, String username, String password) {
        Integer update = userMapper.update(new User(id, username, password));
        if (update>0){
            return new ResultUtils(200,"更改成功",null);
        }
        return new ResultUtils(500,"更改失败",null);
    }

    @Override
    public ResultUtils select() {
        List<User> users = userMapper.select();
        if (users!=null){
            return new ResultUtils(200,"查询成功",users);
        }
         return new ResultUtils(500,"查询失败",null);
    }
}
